<template>
    <div class="chat-wrap">
        <div class="chat-head">聊天消息</div>
        <ul class="chat-body">
            <li v-for="item in list">{{item.page}}->{{item.text}}</li>
        </ul>
        <div class="chat-foot">
            <input type="text" class="foot-input" v-model="msg"/>
            <div class="foot-button" @click="sendTap">发送</div>
        </div>
    </div>
</template>

<script setup>
    import {onBeforeMount, defineProps, ref, unref} from "vue"
    import {sendMsg, addListenerMsg} from "/src/utils"

    const props = defineProps(['page'])

    let list = ref([])
    let msg = ref("")

    const sendTap = () => {
        sendMsg({page: props.page || "", cmd: "msg", text: unref(msg)})
        msg.value = ""
    }

    onBeforeMount(() => {

        addListenerMsg((request, sender, sendResponse) => {
            console.log('chat', request)
            unref(list).push(request)
        })

    })
</script>

<style lang="less" scoped>
    @radius: 4px;

    .chat-wrap {
        width: 200px;
        height: 300px;
        box-shadow: 0 0 10px #ddd;
        border-radius: @radius;
        display: flex;
        flex-direction: column;

        .chat-head {
            padding: 5px;
        }

        .chat-body {
            flex: 1;
            padding: 5px;
        }

        .chat-foot {
            padding: 5px;
            display: flex;
            justify-content: space-between;

            .foot-input {
                width: 125px;
                height: 28px;
                line-height: 28px;
                border: 1px solid #ddd;
                border-radius: @radius;
                outline: none;
                font-size: 14px;
            }

            .foot-button {
                width: 60px;
                height: 30px;
                line-height: 30px;
                border: none;
                outline: none;
                background-color: #1e64de;
                color: white;
                text-align: center;
                border-radius: @radius;
                cursor: pointer;
                font-size: 14px;
            }
        }
    }
</style>